//
//  AddContentViewController.m
//  Flava
//
//  Created by Ug-Code on 13-8-19.
//  Copyright (c) 2013年 UgCode. All rights reserved.
//

#import "AddContentViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface AddContentViewController ()

@end

@implementation AddContentViewController

@synthesize
navigationBar = _navigationBar,
titleText = _titleText,
contentText = _contentText,
date = _date,
dayButton = _dayButton,
timeButton = _timeButton,
dayPicker = _dayPicker,
timePicker = _timePicker,
noteModel = _noteModel;

- (void)viewDidLoad
{
    [super viewDidLoad];
	
	[self createBackgroundView];
	[self createNavigationBar];
	[self createLeftButton];
	[self createRightButton];
	[self createTextField];
	[self createTextView];
	[self createPickerView];
	[self createkeyBoardView];
	[self createContentModel];
	
	//给键盘注册监听时间
	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(keyBoardWillShow:)
												 name:UIKeyboardWillShowNotification
											   object:nil];
	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(keyBoardWillHidden:)
												 name:UIKeyboardWillHideNotification
											   object:nil];
	//注册键盘上部窗口按钮点击事件监听
	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(keyBoardButtonClick:)
												 name:@"FlavaKeyBoardViewButton"
											   object:nil];
	//注册日期改变监听
	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(dateUpdate:)
												 name:@"ContentDateUpdate"
											   object:nil];
}

- (void)leftButtonClick
{
	CATransition *animation = [CATransition animation];
	animation.duration = 0.5f;
	animation.type = @"cube";
	animation.subtype = kCATransitionFromRight;
	[self.navigationController.view.layer addAnimation:animation forKey:nil];
	
	[self.navigationController popViewControllerAnimated:YES];
}

/** 保存按钮事件 */
- (void)saveButtonClick
{
	//-- 内容文字必须不能为空
	if (_contentText.text != nil) {
		_noteModel.titleText = _titleText.text;
		_noteModel.contentText = _contentText.text;
		_noteModel.reviseDate = _date;
		NSLog(@"\n title = %@ \n content = %@ \n reviseData = %@ \n createData = %@ \n", _noteModel.titleText, _noteModel.contentText, _noteModel.reviseDate, _noteModel.createDate);
		NSLog(@"%@", _date);
		
		
		
		
	} else {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告"
														message:@"请输入文字"
													   delegate:nil
											  cancelButtonTitle:@"确定"
											  otherButtonTitles:nil, nil];
		[self.view addSubview:alert];
		[alert show];
	}
}

/** 键盘上窗口的按钮点击方法
 */
- (void)keyBoardButtonClick:(NSNotification *)obj
{
	UGButton *button = obj.object;
	switch (button.tag) {
		case 1: {
			NSLog(@"%d", button.tag);
			AttachPhotoViewController *attachPhotoVC = [[AttachPhotoViewController alloc] init];
			[self presentViewController:attachPhotoVC animated:YES completion:Nil];
		}
			break;
		case 2:
			NSLog(@"%d", button.tag);
			break;
		case 3:
			NSLog(@"%d", button.tag);
			break;
		case 4:
			NSLog(@"%d", button.tag);
			break;
		case 5:
			NSLog(@"%d", button.tag);
			break;
		case 6:
			NSLog(@"%d", button.tag);
			break;
		case 7:
			NSLog(@"%d", button.tag);
			break;
		case 8:
			NSLog(@"%d", button.tag);
			break;
		case 98:
			NSLog(@"%d", button.tag);
			break;
		case 99:
			NSLog(@"%d", button.tag);
			break;
		default:
			break;
	}
	
	button.selected = YES;
}

- (void)dateUpdate:(NSNotification *)obj
{
	self.date = obj.object;
}

-(void)keyBoardWillHidden:(NSNotification *)not{
	[UIView animateWithDuration:0.3 animations:^{
		_keyBoardView.frame = CGRectMake(0,[UGDevice windowHeight],320,55);
	}];
}


-(void)keyBoardWillShow:(NSNotification *)not{
	//获得键盘的高度（size）
	CGSize size =[[not.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    //设定一个动画，frame在动画效果时间内改变
    [UIView animateWithDuration:0.3
                     animations:^{
						 //动画有效时间范围内要执行的代码
                         _keyBoardView.frame = CGRectMake(0,[UGDevice windowHeight]-size.height-75,320,55);
                     }];
}

- (void)createNavigationBar
{
	_navigationBar = [[NavigationBar alloc] init];
	[self.view addSubview:_navigationBar];
//	_navigationBar.shadow = YES;
}

- (void)createLeftButton
{
	UGButton *button = [[UGButton alloc] init];
	button.frame = CGRectMake(5, 6, 55, 33);
	[button setNormalImageWithFile:@"navigation_bar_btn_common_n.png"];
	[button setHighlightedImageWithFile:@"navigation_bar_btn_common_h.png"];
	[button setNormalTitle:@"取消"];
	[button setNormalTitleColor:[UIColor colorWithRed:105.0f/255 green:105.0f/255 blue:105.0f/255 alpha:1]];
	button.titleLabel.font = [UIFont boldSystemFontOfSize:12];
	[button addTarget:self action:@selector(leftButtonClick) forControlEvents:UIControlEventTouchUpInside];
	[self.navigationBar addSubview:button];
}

- (void)createRightButton
{
	UGButton *button = [[UGButton alloc] init];
	button.frame = CGRectMake(250, 6, 55, 33);
	[button setNormalImageWithFile:@"navigation_bar_btn_red_n.png"];
	[button setHighlightedImageWithFile:@"navigation_bar_btn_red_h.png"];
	[button setNormalTitle:@"保存"];
	button.titleLabel.font = [UIFont boldSystemFontOfSize:12];
	[button addTarget:self action:@selector(saveButtonClick) forControlEvents:UIControlEventTouchUpInside];
	[self.navigationBar addSubview:button];
}

- (void)createTextField
{
	_titleText = [[UITextField alloc] init];
	_titleText.delegate = self;
	_titleText.returnKeyType = UIReturnKeyNext;
	_titleText.frame = CGRectMake(69, 11, 170, 22);
	_titleText.borderStyle = UITextBorderStyleNone; //无边框
	_titleText.textAlignment = NSTextAlignmentCenter; //居中
	_titleText.font = [UIFont systemFontOfSize:17];
	_titleText.adjustsFontSizeToFitWidth = YES;
	_titleText.minimumFontSize = 14;
	_titleText.placeholder = @"标题";
	_titleText.clearButtonMode = UITextFieldViewModeWhileEditing;
	
	[self.navigationBar addSubview:_titleText];
}

- (void)createBackgroundView
{
	UIImage *bgImage = [UIImage imageNamed:@"text_view_bg.png"];
	UIImageView *footerView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"footer_bg1.png"]];
	footerView.frame = CGRectMake(0, [UGDevice windowHeight]-64-216, 320, 216);
	bgImage = [bgImage stretchableImageWithLeftCapWidth:1 topCapHeight:1];
	_bgView = [[UGView alloc] init];
	
	if ([UGDevice systemVersion] >= 7) {
		_bgView.frame = CGRectMake(0, 64, 320, [UGDevice windowHeight]-64);
	} else {
		_bgView.frame = CGRectMake(0, 44, 320, [UGDevice windowHeight]-64);
	}
	_bgView.backgroundImage = bgImage;
	[_bgView addSubview:footerView];
	[self.view addSubview:_bgView];
}

- (void)createTextView
{
	_textController = [[AddTextViewController alloc] init];
	_contentText = _textController.contentText;
	[_bgView addSubview:_contentText];
}

- (void)createPickerView
{
	_PickerController = [[AddDatePickerViewController alloc] init];
	_PickerController.titleText = _titleText;
	_PickerController.bodyText = _contentText;
	//初始时间
	_date = [NSDate date];
	//
	_dayButton = _PickerController.dayButton;
	_timeButton = _PickerController.timeButton;
	//
	_dayPicker = _PickerController.dayPicker;
	_timePicker = _PickerController.timePicker;
	//
	_shadowView = _PickerController.shadowView;
	//
	[_bgView addSubview:_dayButton];
	[_bgView addSubview:_timeButton];
	[_bgView addSubview:_dayPicker];
	[_bgView addSubview:_timePicker];
	[self.view addSubview:_shadowView];
	
}

- (void)createkeyBoardView
{
	_keyBoardView = [[KeyBoardView alloc] init];
	_keyBoardView.frame = CGRectMake(0, [UGDevice windowHeight], 320, 55);
	[self.view addSubview:_keyBoardView];
}

- (void)createContentModel
{
	_noteModel = [[NoteModel alloc] init];
}

#pragma mark - textFiledDeleget

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
	[_contentText becomeFirstResponder];
	return YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
